package com.yzdmw.toutiao.history;

import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.lifecycle.ViewModelProvider;

import android.os.Bundle;
//import android.support.v4.view.ViewPager;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavDirections;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager.widget.ViewPager;


//import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;

import com.google.android.material.tabs.TabLayout;
import com.yzdmw.toutiao.R;
import com.yzdmw.toutiao.wheeldisk.ui.WheeldiskFragmentDirections;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class HistoryFragment extends Fragment {
    public static final String TAG = "HistoryFragment";
    public static final boolean DEBUG = true;

    private HistoryViewModel mViewModel;
    private ViewPager pager;
    private FragmentAdapter fragmentAdapter;
    private List<ItemHistoryFragment> fragmentList;
    private TabLayout tabLayout;
    private List<String> mTitles;
    private String [] title={"历史","点赞","评论"};

    public static HistoryFragment newInstance() {
        return new HistoryFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v=inflater.inflate(R.layout.history_fragment, container, false);
        pager = v.findViewById(R.id.page);
        tabLayout = v.findViewById(R.id.tab_layout);

        return v;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = new ViewModelProvider(this).get(HistoryViewModel.class);
        // TODO: Use the ViewModel
        fragmentList=new ArrayList<>();
        mTitles=new ArrayList<>();
        for(int i=0;i<title.length;i++){
            mTitles.add(title[i]);
            ItemHistoryFragment itemHistoryFragment = new ItemHistoryFragment(title[i]);
            fragmentList.add(itemHistoryFragment);
        }

        fragmentAdapter=new FragmentAdapter(getActivity().getSupportFragmentManager(),fragmentList,mTitles);
        pager.setAdapter(fragmentAdapter);
        tabLayout.setupWithViewPager(pager);//与ViewPage建立关系

        String tab = ((HistoryActivity)requireActivity()).defaultTab;
        int index =  Arrays.asList(title).indexOf(tab);
        if(DEBUG) Log.d(TAG,"tab:"+tab);
        if (tab!=null && index != -1)
            pager.setCurrentItem(index);

    }
    class FragmentAdapter extends FragmentPagerAdapter {


        private List<ItemHistoryFragment> mFragmentList;//各导航的Fragment
        private List<String> mTitle; //导航的标题

        public FragmentAdapter(FragmentManager fragmentManager, List<ItemHistoryFragment>fragments, List<String>title){
            super(fragmentManager,BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
            mFragmentList=fragments;
            mTitle=title;

        }
        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }
        @Override
        public CharSequence getPageTitle(int position) {
            return mTitle.get(position);
        }


    }

}